package com.chinasi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinasi.dao.DictDao;
import com.chinasi.dao.DictInfoDao;
import com.chinasi.model.Dict;
import com.chinasi.model.DictInfo;
import com.chinasi.service.DictService;

@Service
public class DictServiceImpl implements DictService {

	/**
	 * DictDAO
	 */
	@Autowired
	private DictDao DictDao;
	
	@Autowired
	private DictInfoDao DictInfoDao;

	@Override
	public Dict findByCode(String code) {
		Dict result = null;
		Dict searchBean = new Dict();
		searchBean.setCode(code);
		List<Dict> list = DictDao.findByCondition(searchBean);
		if(list != null && list.size() > 0){
			result = list.get(0);
			loadDictInfo(result);
		}
		return result;
	}

	private void loadDictInfo(Dict Dict) {
		DictInfo searchBean = new DictInfo();
		searchBean.setDictoid(Dict.getDictoid());
		Dict.setDictInfoList(DictInfoDao.findByCondition(searchBean));
	}

	@Override
	public List<Dict> findAll() {
		List<Dict> result = null;
		result = DictDao.findByCondition(new Dict());
		if(result != null && result.size() > 0){
			for(Dict item : result){
				loadDictInfo(item);
			}
		}
		return result;
	}
}
